package com.project.controller.web;

import com.project.entity.Major;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.MajorService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 13:06
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/major")
@Api(description = "专业控制",value = "major")
public class MajorController extends BaseService {
    @Autowired
    MajorService majorService;

    /**
     * TateBrown 2018年5月27日
     *
     * @param departmentid
     * @return
     */
    @ApiOperation("根据系id查找专业")
    @GetMapping("/findBath/{departmentid}")
    public R findBatch(@PathVariable("departmentid") Integer departmentid) {
        try {
            List<Major> majors =  majorService.findBatch(departmentid);;
            return R.ok("查询成功").put("data",majors);
        } catch (Exception e) {
            return R.error();
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param major
     * @return
     */
    @ApiOperation("添加单个专业")
    @PostMapping("/addOne")
    public R addOne(@RequestBody Major major) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (majorService.checkMajorIsExist(major)) {
                return R.error("该专业已存在");
            } else {
                majorService.addOne(major);
                return R.ok("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param major
     * @return
     */
    @ApiOperation("修改单条专业记录")
    @PostMapping("/modifyOneById")
    public R modifyOneById(@RequestBody Major major){
        try {
            if (checkDeleteUpdateIsSuccess(majorService.modify(major))){
                return R.ok("修改成功");
            }
            else{
               return R.error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
           return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation("根据专业级联删除")
    @GetMapping("/removeOneChainById/{id}")
    public R removeOneChainById(@PathVariable("id") Integer id, HttpServletRequest request){
        try {
            majorService.removeOneChainById(id, request);
            return R.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据专业表主键查找单个系")
    @GetMapping("/findOneById/{id}")
    public R findOneById(@PathVariable("id") Integer id){
        try {
            return R.ok("查找成功").put("data",majorService.findOne(id));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
